package qsbk.app.play.common;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class TopicPool {

	private String[] topics = {
			"老司机", "大摇大摆", "东施效颦", "狗急跳墙", 
			"芭蕾舞", "奥特曼", "小沈阳", "大象", 
			"打麻将", "猩猩", "占座", "摩托车", 
			"小兔子", "遛狗", "摔倒", "狗屁不通", 
			"大手大脚", "垂头丧气", "美女", "放屁", 
			"打架", "接吻", "嚎啕大哭", "张牙舞爪", 
			"太极", "囧", "回眸一笑", "然并卵",
			"葛优躺", "蓝瘦香菇", "小目标", "套路",
			"吃瓜群众", "洪荒之力", "猴赛雷", "宝宝心里苦",
			"吃藕", "吃土", "高考", "小清新", "神马",
			"呵呵", "开黑", "日了狗", "我也是醉了"
	};
	
	private List<String> filters;
	private List<String> words;
	
	public TopicPool() {
		filters = new ArrayList<>();
		words = new ArrayList<>();
		for (int i = 0; i < topics.length; i++) {
			String topic = topics[i];
			for (int j = 0; j < topic.length(); j++) {
				String word = String.valueOf(topic.charAt(j));
				if (!word.equals("") && !words.contains(word)) {
					words.add(word);
				}
			}
		}
	}
	
	public List<String> getTopics() {
		int length = topics.length;
		if (filters.size() >= length) {
			return null;
		}

		int topicSize = 4;
		if (filters.size() + topicSize > length) {
			topicSize = length - filters.size();
		}
		
		List<String> result = new ArrayList<>(topicSize);
		
		Random random = new Random();
		int next;
		do {
			next = random.nextInt(length);
			String topic = topics[next];
			if (!filters.contains(topic)) {
				filters.add(topic);
				
				result.add(topic);
			}
		} while (result.size() < topicSize);
		return result;
	}
	
	public List<String> generateGuessWords(String topic) {
		if (topic == null || topic.length() == 0) {
			return null;
		}
		
		List<String> words = new ArrayList<>();
		for (int i = 0; i < topic.length(); i++) {
			String word = String.valueOf(topic.charAt(i));
			words.add(word);
		}
		
		int wordSize = this.words.size();
		int next;
		Random random = new Random();
		while (words.size() < 24) {
			next = random.nextInt(wordSize);
			String word = this.words.get(next);
			if (!words.contains(word)) {// 去重
				words.add(word);
			}
		}
		
		Collections.shuffle(words);// 随机一下
		
		return words;
	}

	public void reset() {
		filters.clear();
	}
	
}
